"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EcsWorldSnapshot = exports.EcsGroupSnapshot = exports.EcsEntitySnapshot = exports.EcsComponentSnapshot = void 0;
var EcsComponent_1 = require("./EcsComponent");
/**组件快照 */
var EcsComponentSnapshot = /** @class */ (function () {
    function EcsComponentSnapshot() {
    }
    EcsComponentSnapshot.fromComponent = function (comp) {
        var snapshot = new EcsComponentSnapshot();
        snapshot.compId = EcsComponent_1.EcsComponent.getId(comp);
        snapshot.data = JSON.parse(JSON.stringify(comp));
        return snapshot;
    };
    return EcsComponentSnapshot;
}());
exports.EcsComponentSnapshot = EcsComponentSnapshot;
/**实体快照 */
var EcsEntitySnapshot = /** @class */ (function () {
    function EcsEntitySnapshot() {
        this.components = [];
    }
    EcsEntitySnapshot.fromEntity = function (entity) {
        var snapshot = new EcsEntitySnapshot();
        snapshot.id = entity.id;
        snapshot.groupId = entity._factory.groupId;
        entity.componentMap.forEach(function (comp) {
            snapshot.components.push(EcsComponentSnapshot.fromComponent(comp));
        });
        return snapshot;
    };
    return EcsEntitySnapshot;
}());
exports.EcsEntitySnapshot = EcsEntitySnapshot;
/**实体快照 */
var EcsGroupSnapshot = /** @class */ (function () {
    function EcsGroupSnapshot() {
        this.entityIds = [];
        this.compIds = [];
        this.compIdMap = {};
        this.indexMap = {};
    }
    EcsGroupSnapshot.fromGroup = function (group) {
        var snapshot = new EcsGroupSnapshot();
        snapshot.entityIds = group.entities.map(function (e) { return e.id; });
        snapshot.compIds = group.compIds;
        snapshot.compIdMap = group.compIdMap;
        snapshot.indexMap = group.indexMap;
        return snapshot;
    };
    return EcsGroupSnapshot;
}());
exports.EcsGroupSnapshot = EcsGroupSnapshot;
/**世界快照 */
var EcsWorldSnapshot = /** @class */ (function () {
    function EcsWorldSnapshot() {
        this.singletons = [];
        this.entities = [];
        this.groups = [];
    }
    EcsWorldSnapshot.fromWorld = function (world) {
        var snapshot = new EcsWorldSnapshot();
        snapshot.frame = world.frameCount;
        snapshot.nextEntityGenId = world.nextEntityGenId;
        snapshot.left_elapsed = world.leftElapsed;
        for (var index = 0; index < world.singletons.length; index++) {
            var singleton = world.singletons[index];
            if (singleton) {
                snapshot.singletons.push(JSON.parse(JSON.stringify(singleton)));
            }
        }
        for (var key in world.entities) {
            if (Object.prototype.hasOwnProperty.call(world.entities, key)) {
                var entity = world.entities[key];
                snapshot.entities.push(EcsEntitySnapshot.fromEntity(entity));
            }
        }
        world.groupMap.forEach(function (group) {
            snapshot.groups.push(EcsGroupSnapshot.fromGroup(group));
        });
        return snapshot;
    };
    /**还原世界 */
    EcsWorldSnapshot.restoreWorld = function (world, snapshot) {
        for (var index = 0; index < snapshot.singletons.length; index++) {
            Object.assign(world.singletons[index], snapshot.singletons[index]);
        }
        var _loop_1 = function (key) {
            if (Object.prototype.hasOwnProperty.call(snapshot.entities, key)) {
                var entitySnapshot = snapshot.entities[key];
                var entity_1 = world.entities[entitySnapshot.id];
                var compClzs = entitySnapshot.components.map(function (compSnapshot) { return EcsComponent_1.EcsComponent.getComponentType(compSnapshot.compId); });
                entity_1 = world.addEntityWithComps(compClzs, null, entitySnapshot.id);
                entitySnapshot.components.forEach(function (compSnapshot) {
                    var comp = entity_1.componentMap.get(compSnapshot.compId);
                    Object.assign(comp, compSnapshot.data);
                });
                // for (let index = 0; index < entitySnapshot.components.length; index++) {
                //     const compSnapshot = entitySnapshot.components[index];
                //     // entity.__comps[compSnapshot.compId] = comp;
                //     // entity.__componentsID += Math.pow(2, compSnapshot.compId);
                // }
            }
        };
        for (var key in snapshot.entities) {
            _loop_1(key);
        }
        snapshot.groups.forEach(function (groupSnapshot) {
            var group = world.getGroupByCompIds.apply(world, groupSnapshot.compIds);
            group.compIds = groupSnapshot.compIds;
            group.compIdMap = groupSnapshot.compIdMap;
            group.indexMap = groupSnapshot.indexMap;
            group.entities.length = 0;
            groupSnapshot.entityIds.forEach(function (id) {
                group.entities.push(world.entities[id]);
            });
        });
        world.frameCount = snapshot.frame;
        world.leftElapsed = snapshot.left_elapsed;
        world.systems.forEach(function (s) { return s.updateCount = world.frameCount; });
        world.nextEntityGenId = snapshot.nextEntityGenId;
    };
    return EcsWorldSnapshot;
}());
exports.EcsWorldSnapshot = EcsWorldSnapshot;
